package com.alibaba.boot.netty.netty.http;

import com.alibaba.boot.netty.netty.NettyServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class TestServer {


    public static void main(String[] args) {
        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        //创建服务器端启动对象，配置参数
        ServerBootstrap bootstrap;
        try {
            bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128) //设置线程队列等待连接个数
                    .childHandler(new TestServerInit());
            //监听一个端口
            ChannelFuture channelFuture = bootstrap.bind(30001).sync();
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    System.out.println("....http server is  ready");

                }
            });
            //对关闭通道进行监听
            channelFuture.channel().closeFuture().sync();

        }catch (Exception e){
            workerGroup.shutdownGracefully();
        }
    }
}
